package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.util.R;
import com.example.buildtable.pojo.Notice;
import com.example.buildtable.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "公告")
@RequestMapping(value = "/notice/")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @Operation(summary = "查询所有公告数据")
    @GetMapping(value = "/getAllNotice")
    public R getAllNotice() {
        List<Notice> noticeList = noticeService.list();
        return R.ok("查询所有公告数据成功").data("noticeList", noticeList);
    }

    @Operation(summary = "查询所有公告数据模糊查询带分页")
    @PostMapping(value = "/getNoticePage/{page}/{limit}")
    public R getNoticePage(@PathVariable("page") Long page, @PathVariable("limit") Long limit,  @RequestParam(name = "noticeTitle", required = false) String noticeTitle) {
        LambdaJoinQueryWrapper<Notice> wrapper = new LambdaJoinQueryWrapper<>(Notice.class);
        wrapper.like(noticeTitle != null && !noticeTitle.equals(""), Notice::getNoticeTitle, noticeTitle);
        Page<Notice> noticePage = new Page<>(page, limit);
        noticeService.page(noticePage, wrapper);
        //每页显示的记录
        List<Notice> noticeList = noticePage.getRecords();
        //共多少条记录
        Long total = noticePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", noticeList);
        return R.ok("查询所有公告数据模糊查询带分页成功").data("map", map);
    }
    
    @Operation(summary = "增加公告")
    @PostMapping(value = "/addNotice")
    public R addNotice(@RequestBody Notice notice) {
        boolean flag = noticeService.save(notice);
        if (flag) {
            return R.ok("增加公告成功");
        } else {
            return R.ok("增加公告失败");
        }
    }

    @Operation(summary = "删除公告")
    @GetMapping(value = "/deleteNotice/{noticeId}")
    public R deleteNotice(@PathVariable("noticeId") Integer noticeId) {
        boolean flag = noticeService.removeById(noticeId);
        if (flag) {
            return R.ok("删除公告成功");
        } else {
            return R.ok("删除公告失败");
        }
    }

    @Operation(summary = "按照公告查看")
    @GetMapping(value = "/getNoticeById/{noticeId}")
    public R getNoticeById(@PathVariable("noticeId") Integer noticeId) {
        Notice notice = noticeService.getById(noticeId);
        if (notice != null) {
            return R.ok("按照公告查看成功").data("notice", notice);
        } else {
            return R.error("按照公告查看失败");
        }
    }

    @Operation(summary = "修改公告")
    @PostMapping(value = "/updateNotice")
    public R updateNotice(Notice notice) {
        boolean flag = noticeService.updateById(notice);
        if (flag) {
            return R.ok("修改公告成功");
        } else {
            return R.ok("修改公告失败");
        }
    }

    @Operation(summary = "修改公告的状态")
    @PostMapping(value = "/updateNoticeStatus")
    public R updateNoticeStatus(@RequestBody Notice notice) {
        Integer status = notice.getStatus();
        if (status == 1 || status == 0) {
            int newStatus = (status == 1) ? 0 : 1;
            notice.setStatus(newStatus);
            boolean updated = noticeService.updateById(notice);

            String statusText = null;
            if (updated) {
                statusText = (newStatus == 1) ? "可用" : "不可用";
                return R.ok(statusText + notice.getNoticeTitle() + "成功").data("notice", notice);
            } else {
                return R.error(statusText + notice.getNoticeTitle() + "失败");
            }
        } else {
            return R.error("无效的状态值");
        }
    }
}